﻿<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <link href="../lib/layui/css/layui.min.css" rel="stylesheet" />
    <style>
        .layui-form-select dl {
            max-height: 200px;
        }
    </style>
    <script type="text/javascript" src="../lib/Base64/base64.min.js"></script>
    <script type="text/javascript" src="../linq.js/linq.js"></script>
    <script src="../lib/layui/layui.js"></script>
</head>
<body>
    <div class="layui-fluid">
        <form class="layui-form" lay-filter="userRoleForm">
            <input type="hidden" id="id" name="id" />
            <div class="layui-form-item" style="margin-top:10px;">
                <label class="layui-form-label">所属角色</label>
                <div class="layui-input-block">
                    <select name="rid" id="rid" lay-filter="rid" lay-verify="required"></select>
                </div>
            </div>
            <div class="layui-form-item" style="text-align:center;">
                <a class="layui-btn" href="javascript:;" lay-submit lay-filter="saveuserrole">保存</a>
            </div>
        </form>

    </div>
    <script type="text/javascript">
        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        };

        layui.use(['form'], function () {
            var form = layui.form;
            var $ = layui.$;
            $(function () {
                //加载角色下拉框数据
                $("#id").val(getrequest().id);
                $.ajax({
                    type: 'get',
                    url: localStorage.getItem('hosttingaddr') + '/api/organization/getroles?did=' + getrequest().did,
                    async: false,
                    success: function (re) {
                        if (re.code == 0) {
                            $("#rid").append("<option value=''>请选择</option>");
                            for (var i = 0; i < re.data.length; i++) {
                                $("#rid").append("<option value='" + re.data[i].id + "'>" + re.data[i].name + "</option>");
                            }
                            form.render();
                        }
                    }
                });

                form.val('userRoleForm', {
                    "id": getrequest().id,
                    "rid": getrequest().rid
                })
                form.render();
            });
            
            //保存用户角色信息
            form.on('submit(saveuserrole)', function (data) {
                var data = data;
                data.elem.removeAttribute("lay-submit");
                var i = layui.layer.load();
                layui.$.ajax({
                    type: 'post',
                    url: localStorage.getItem('hosttingaddr') + '/api/UserAuthority/saveuserrole',
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify(data.field),
                    success: function (re) {
                        layui.layer.close(i);
                        layer.alert(re.code == 0 ? "保存成功" : re.msg, { icon: re.code == 0 ? 1 : 5 }, function (i) {
                            parent.layui.table.reload('userTable');
                            var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                            parent.layer.close(index);
                        });
                    },
                    error: function (data) {
                        layui.layer.close(i);
                        layer.alert("保存失败", { icon: 5}, function (i) {
                            parent.layui.table.reload('userTable');
                            var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                            parent.layer.close(index);
                        });
                    }
                });
                return false;
            });




        })
    </script>
</body>
</html>